<?php

namespace App\Service;

use App\Model\AccountIntegralModel;
use App\Model\ActiveJoinModel;
use App\Model\ActiveModel;
use App\Model\ExamineRepairModel;
use App\Model\OrderModel;
use App\Model\ShareLatticeGoodsModel;
use App\Model\ShareLatticeOrderModel;
use App\Model\TradePropertyModel;
use App\Utility\Payment;
use EasySwoole\Actor\Exception\InvalidActor;
use EasySwoole\Mysqli\Exception\Exception;
use EasySwoole\Pay\Exceptions\InvalidArgumentException;
use EasySwoole\Pay\Exceptions\InvalidSignException;
use Throwable;

class CallbackService extends BaseService
{
    /**
     * @param string $data
     * @return array
     * @throws Exception
     * @throws InvalidActor
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws InvalidArgumentException
     * @throws InvalidSignException
     */
    public function mxwepay(string $data): array
    {
        $appid = ConfigService::getInstance()->getSys("mxappid", 'wepay');
        $mxappid = ConfigService::getInstance()->getSys("mxminiappid", 'wepay');
        $mchid = ConfigService::getInstance()->getSys("mchid", 'wepay');
        $password = ConfigService::getInstance()->getSys("mxpassword", 'wepay');
        $notifyUrl = ConfigService::getInstance()->getSys("wenotify_url", 'wepay');
        $res = Payment::getInstance()->mxWepayCallback($appid, $mxappid, $mchid, $password, $notifyUrl, $data);
        if ($res->get('result_code') && $res->get('result_code') == 'SUCCESS') {
            $order_no = $res->get('out_trade_no');

            $order = OrderModel::create()->getOrder(['order_no' => $order_no, 'status' => 3]);
            if (!$order) {
                return [
                    'result' => false,
                    'msg' => '订单不存在'
                ];
            }
            $data = [
                'status' => 4,
                'payment_amount' => $res->get('cash_fee') * 0.01,
                'payment_remark' => $res->get('return_code'),
                'payment_status' => 1,
                'payment_trade' => $res->get('transaction_id'),
                'payment_time' => strtotime($res->get('time_end'))
            ];
            $result = OrderModel::create()->update($data, ['order_no' => $order_no]);
            if (!$result) return [
                'result' => false,
                'msg' => 'fail'
            ];
            $memo = $res->toXML();

            switch ($order->type) {
                case 1:
                    $log = $this->property($order);
                    break;
                case 2:
                    $log = $this->park($order);
                    break;
                case 3:
                    $log = $this->repair($order);
                    break;
                case 4:
                    $log = $this->active($order);
                    break;
                case 7:
                    $log = $this->lattice($order);
                    break;
                default:
                    $log = false;
            }

            if ($log) {
                $par = [
                    'uid' => $order->uid,
                    'bill_type' => 1,
                    'mold' => 1,
                    'amount' => $order->amount_real,
                    'static' => $order->amount_real,
                    'memo' => $order->order_remark
                ];

                AccountIntegralModel::create()->addAccount($par);
            };
            return [
                'result' => true,
                'msg' => 'success',
                'data' => $res
            ];
        }
        return [
            'result' => false,
            'msg' => 'fail'
        ];
    }

    /**
     * @param $order
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function property($order): bool
    {
        $res = TradePropertyModel::create()->update(['status' => 1], ['order_no' => $order->order_no, 'uid' => $order->uid]);
        if ($res) return true;
        return false;
    }

    public function park($order): bool
    {
        return false;
    }

    /**
     * @param $order
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function repair($order): bool
    {
        $exRes = ExamineRepairModel::create()->update(['status' => 3], ['uid' => $order->uid, 'pid' => $order->rid]);
        if ($exRes) return true;

        return false;
    }

    /**
     * @param $order
     * @return bool
     * @throws Exception
     * @throws InvalidActor
     * @throws Throwable
     * @throws Exception
     */
    public function active($order): bool
    {
        $upRes = ActiveJoinModel::create()->update(['status' => 1], ['uid' => $order->uid, 'gid' => $order->rid]);
        $join = ActiveJoinModel::create()->getActive(['uid' => $order->uid, 'gid' => $order->rid]);
        $active = ActiveModel::create()->getActive(['id' => $order->rid]);

        $upRes = ActiveModel::create()->update(['join' => $active->join + $join->num], ['id' => $order->rid]);
        if ($upRes) return true;

        return false;
    }

    /**
     * @param $order
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function lattice($order): bool
    {
        $res = ShareLatticeGoodsModel::create()->update(['status' => 1], ['id' => $order->rid]);

        $res1 = ShareLatticeOrderModel::create()->update(['status' => 3], ['gid' => $order->rid]);
        if (!$res || !$res1) return false;
        return true;

    }
}